package com.example.druid;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class DruidExample {

    public static void main(String[] args) throws Exception {
        // 加载配置文件
        Properties properties = new Properties();
        InputStream input = DruidExample.class.getClassLoader().getResourceAsStream("druid.properties");
        properties.load(input);
        // 获取连接池对象
        DataSource  dataSource = DruidDataSourceFactory.createDataSource(properties);
        //获取到连接
        Connection connection = dataSource.getConnection();
        //获取到执行SQL的对象，并执行SQL语句
        String sql ="SELECT * FROM account WHERE id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,2);

        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            double money = resultSet.getDouble("money");
            System.out.println("id: "+id+" ,name: "+name+" ,money: "+money);

        }


    }
}
